<template>
  <div>
    <!-- 头部 -->
    <div class="search-box" @click="$router.replace('/search')">
      <div class="search-item">
        <van-icon size="18" class="tubiao" name="search" />
        <span>搜索鲜花、蛋糕、礼品</span>
      </div>
    </div>
    <!-- /头部 -->
    <!-- 主体 -->
    <div class="container">
        <!-- 左侧 -->
      <div class="title-wrap">
        <div class="title">
          <div :class="['item', { 'active': activeIndex === index }]"
           v-for="(cate, index) in cates"
           :key="index"
           @click="active(index, cate.id)"
            >{{cate.name}}</div>
        </div>
      </div>
          <!-- /左侧 -->
          <!-- 右侧 -->
      <div class="content-wrap">
        <div class="contents">
          <div class="content" v-for="item in items" :key="item.id">
            <img :src="item.pic" alt="">
            <div class="text">
              <h4>{{item.name}}</h4>
              <span>{{item.characteristic?item.characteristic:'暂无描述'}}</span>
              <div class="price">
                <p><van-icon size="20" class="￥" name="gold-coin-o"/>123</p>
                <van-icon size="20" name="cart" />
              </div>
            </div>
          </div>
        </div>
      </div>
          <!-- /右侧 -->
    </div>
    <!-- /主体 -->
    <my-tabbar></my-tabbar>
  </div>
</template>

<script>
import MyTabbar from '../../components/MyTabbar'
import { fetchCate, fetchItems } from '../../api'
export default {
  data () {
    return {
      cates: [],
      items: [],
      activeIndex: 0
    }
  },
  created () {
    this.fetchCate()
  },
  methods: {
    fetchItems (id) {
      fetchItems({
        categoryId: id
      }).then(res => {
        if (res.data.code === 0) {
          this.items = res.data.data.result
        }
      })
    },
    active (index, id) {
      this.activeIndex = index
      this.fetchItems(id)
    },
    fetchCate () {
      fetchCate().then(res => {
        if (res.data.code === 0) {
          this.cates = res.data.data
          this.fetchItems(this.cates[0].id)
        }
      })
    }
  },
  components: {
    MyTabbar
  }
}
</script>

<style lang="scss" scoped>
.search-box{
  width: 100%;
  height: 46px;
  background-color: #fff;
  display: flex;
  justify-content: center;
  align-items: center;
  .search-item{
    width: 90%;
    height: 38px;
    background-color: #f3f5f7;
    border-radius:20px ;
    line-height: 38px;
    color: #71797f;
    font-size: 14px;
    .tubiao{
      margin-left: 30px;
    }
    span{
      margin-left: 18px;
    }
  }
}
/* 主体部分 */
.container{
  position: fixed;
  left: 0;right: 0;
  top: 48px;
  bottom: 50px;
  background-color: #fff;
  display: flex;
  .title-wrap{
    width: 2.125rem;
    border-right:2px solid #f3f5f7;
    height: 100%;
    overflow-y: auto;
    .title{
    font-size: 14px;
    .item{
      line-height: 56px;
      text-align: center;
      color:#232628;
      position: relative;
      &.active{
        color: #ff734c;
        &::after{
          content: "";
          height: 30px;
          width: 2px;
          background-color: #ff734c;
          position: absolute;
          left: 0px;
          top: 25%;
        }
      }
    }
  }
  }
  .content-wrap{
    flex: 1;
    height: 100%;
    overflow-y: auto;
    margin: 0px 8px;
    .contents{
      width: 100%;
      .content{
        border-radius: 12px;
        box-shadow: 12px;
        display: flex;
        margin-top: 10px;
        overflow: hidden;
        img{
          height: 100px;
          width: 100px;
        }
        .text{
          flex: 1;
          margin: 4px 4px;
          h4{
            margin-top: 6px;
            margin-bottom: 8px;
          }
          .price{
            margin-top: 12px;
            display: flex;
            justify-content: space-between;
            color:#ff734c;
            p{
              font-size: 16px;
              line-height: 20px;
              .￥{
                padding-right: 5px;
              }
            }
          }
        }
      }
    }
  }
}
</style>
